package com.hanhai.material.device.controller;

import com.hanhai.material.common.result.Result;
import com.hanhai.material.common.result.ResultTool;
import com.hanhai.material.device.entity.SysMachineAdjustRecord;
import com.hanhai.material.device.service.SysMachineAdjustRecordService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

import static com.hanhai.material.device.entity.table.SysMachineAdjustRecordTableDef.SYS_MACHINE_ADJUST_RECORD;
import static com.hanhai.material.utils.TimeUtils.fromDateToString;

/**
 *  控制层。
 *
 * @author youchuang
 * @since 2024/03/11 13:54:56
 */
@RestController
@RequestMapping("/machine_adjust_record")
@Log4j2
public class SysMachineAdjustRecordController {

    @Autowired
    private SysMachineAdjustRecordService sysMachineAdjustRecordService;

    /**
     * 添加。
     *
     * @param sysMachineAdjustRecord 
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    public boolean save(@RequestBody SysMachineAdjustRecord sysMachineAdjustRecord) {
        return sysMachineAdjustRecordService.save(sysMachineAdjustRecord);
    }

    /**
     * 根据主键删除。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    public boolean remove(@PathVariable Serializable id) {
        return sysMachineAdjustRecordService.removeById(id);
    }

    /**
     * 根据主键更新。
     *
     * @param sysMachineAdjustRecord 
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("update")
    public boolean update(@RequestBody SysMachineAdjustRecord sysMachineAdjustRecord) {
        return sysMachineAdjustRecordService.updateById(sysMachineAdjustRecord);
    }

    /**
     * 查询所有。
     *
     * @return 所有数据
     */
    @PostMapping("list")
    public Result<?> list(@RequestBody SysMachineAdjustRecord machineAdjustRecord) {
        QueryWrapper queryWrapper=QueryWrapper.create();
        queryWrapper.select(SYS_MACHINE_ADJUST_RECORD.MACHINE_NAME,SYS_MACHINE_ADJUST_RECORD.WORK_PLAN_ID,SYS_MACHINE_ADJUST_RECORD.PRODUCT_DATE,SYS_MACHINE_ADJUST_RECORD.DURATION,SYS_MACHINE_ADJUST_RECORD.REASON,SYS_MACHINE_ADJUST_RECORD.ADJUST_PEOPLE,SYS_MACHINE_ADJUST_RECORD.ID);
        queryWrapper.where(SYS_MACHINE_ADJUST_RECORD.MACHINE_NAME.eq(machineAdjustRecord.getMachineName()));
        queryWrapper.where(SYS_MACHINE_ADJUST_RECORD.WORK_PLAN_ID.eq(machineAdjustRecord.getWorkPlanId()));
        queryWrapper.where(SYS_MACHINE_ADJUST_RECORD.SHIFT.eq(machineAdjustRecord.getShift()));
        queryWrapper.where(SYS_MACHINE_ADJUST_RECORD.PRODUCT_DATE.eq(fromDateToString("yyyy-MM-dd",machineAdjustRecord.getProductDate())));
        log.error("调机记录SQL：{}",queryWrapper.toSQL());
        List<SysMachineAdjustRecord> machineAdjustRecordList=this.sysMachineAdjustRecordService.list(queryWrapper);
        return machineAdjustRecordList.isEmpty()? ResultTool.fail():ResultTool.success(machineAdjustRecordList);
    }

    /**
     * 根据主键获取详细信息。
     *
     * @param id 主键
     * @return 详情
     */
    @GetMapping("getInfo/{id}")
    public SysMachineAdjustRecord getInfo(@PathVariable Serializable id) {
        return sysMachineAdjustRecordService.getById(id);
    }

    /**
     * 分页查询。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public Page<SysMachineAdjustRecord> page(Page<SysMachineAdjustRecord> page) {
        return sysMachineAdjustRecordService.page(page);
    }

}
